/**
 * 
 */
package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;

import weavingdraft.PatternSquare;

import Background.Contestant;
import Background.Entry;
import Background.Judge;
import Background.Lists;

/**
 * @author Justin Sorrell
 * @version 02/04/2013
 */
@SuppressWarnings("serial")
public class ListGUI extends JFrame {
	
	/**
	 * A JPanel used when displaying the registered contestants.
	 */
	
	private JPanel contestant_panel;

	/**
	 * A JPanel used when displaying the registered judges.
	 */
	
	private JPanel judge_panel;
	
	/**
	 * A JPanel used when displaying the contest winners.
	 */
	
	private JPanel winners_panel;

	/**
	 * A JPanel used when displaying the list of entries.
	 */
	
	private JPanel entry_panel;
	
	/**
	 * A scroll pane for the selected panel to sit in.
	 */
	
	private JScrollPane scrollPane;
	
	/**
	 * A JLabel that holds the text "Name".
	 */
	
	private JLabel name_header;
	
	/**
	 * A JLabel that holds the text "Email".
	 */
	
	private JLabel email_header;
	
	/**
	 * A JLabel that holds the text "Phone number".
	 */
	
	private JLabel phone_number_header;
	
	/**
	 * A JLabel that holds the text "Registration Number".
	 */
	
	private JLabel reg_number_header;
	
	/**
	 * A JLabel that holds the text "Entry 1".
	 */
	
	private JLabel entry_1_header;
	
	/**
	 * A JLabel that holds the text "Entry 2".
	 */
	
	private JLabel entry_2_header;

	/**
	 * A JLabel that holds the text "Entry 3".
	 */
	
	private JLabel entry_3_header;
	
	/**
	 * An int for the list decider method to use in its switch case block.
	 */
	
	private int choice = 0;
	
	/**
	 * An arrayList that holds a list of contestants.
	 */
	
	private ArrayList<Contestant> contestantList;
	
	/**
	 * An arrayList that holds a list of judges.
	 */
	
	private ArrayList<Judge> judgeList;
	
	/**
	 * An arrayList that holds the list of contest winners.
	 */
	
	private ArrayList<Contestant> contestWinners;

	/**
	 * A dimension used in setting the size of the frame.
	 */
	
	private static final Dimension PANEL_SIZE = new Dimension(1000, 500);
	
	/**
	 * A field for the dimension that specifies the size of the labels.
	 */
	
	private static final Dimension LABEL_SIZE = new Dimension(300, 25);


	/**
	 * A field for the dimension that specifies the size of the labels.
	 */
	
	private static final Dimension ENTRY_LABEL_SIZE = new Dimension(180, 25);

	/**
	 * Constructor for the ListGUI. Gets the current lists, instantiates the panels and sets 
	 * the sizes of those panels.
	 */
	
	public ListGUI() {
		super("Lists");
		contestantList = Lists.getContestantList();
		judgeList = Lists.getJudgeList();
		contestWinners = Lists.getWinners();
		contestant_panel = new JPanel();
		contestant_panel.setPreferredSize(PANEL_SIZE);
		judge_panel = new JPanel();
		judge_panel.setPreferredSize(PANEL_SIZE);
		winners_panel = new JPanel();
		winners_panel.setPreferredSize(PANEL_SIZE);
		entry_panel = new JPanel();
		entry_panel.setPreferredSize(PANEL_SIZE);
		entry_panel.setLayout(new FlowLayout());
		name_header = new JLabel();
		name_header.setPreferredSize(LABEL_SIZE);
		email_header = new JLabel();
		email_header.setPreferredSize(LABEL_SIZE);
		phone_number_header = new JLabel();
		phone_number_header.setPreferredSize(LABEL_SIZE);
		reg_number_header = new JLabel();
		reg_number_header.setPreferredSize(ENTRY_LABEL_SIZE);
		entry_1_header = new JLabel();
		entry_1_header.setPreferredSize(ENTRY_LABEL_SIZE);
		entry_2_header = new JLabel();
		entry_2_header.setPreferredSize(ENTRY_LABEL_SIZE);
		entry_3_header = new JLabel();
		entry_3_header.setPreferredSize(ENTRY_LABEL_SIZE);
		
	}

	/**
	 * Sets up the functionality for the window and calls listDecider to decide which gui to 
	 * display. 
	 */
	
	public void start() {
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setResizable(false);
	    setVisible(true);
	    setSize(PANEL_SIZE);
		listDecider();
	    validate();
	    
	}
	
	/**
	 * Decides which list to display based on the choice sent in from the button's actionlistener.
	 */
	
	private void listDecider() {
		switch(choice){
		case 1: showContestantList();
			break;
		case 2: showJudgeList();
			break;
		case 3: showEntryList();
			break;
		case 4: showContestWinners();
			break;
		}
		
		
	}


	/**
	 * Populates the frame with a list of contestants.  For use by the organizer.
	 */
		
	public void showContestantList() {
		name_header.setText("Name");
		email_header.setText("User Email");
		phone_number_header.setText("Phone Number");
		contestant_panel.add(name_header, BorderLayout.NORTH);
		contestant_panel.add(email_header, BorderLayout.NORTH);
		contestant_panel.add(phone_number_header, BorderLayout.NORTH);
		for (Contestant c : contestantList) {
			JLabel name = new JLabel();
			JLabel email = new JLabel();
			JLabel number = new JLabel();
			name.setPreferredSize(LABEL_SIZE);
			email.setPreferredSize(LABEL_SIZE);
			number.setPreferredSize(LABEL_SIZE);
			name.setText(c.getName());
			email.setText(c.getEmail());
			number.setText(c.getPhoneNumber());
			contestant_panel.add(name);
			contestant_panel.add(email);
			contestant_panel.add(number);
		}// close for loop
		scrollPane = new JScrollPane(contestant_panel);
		add(scrollPane);
		
	}

	/**
	 * Populates the frame with a list of judges.  For use by the organizer.
	 */
		
	public void showJudgeList() {
		name_header.setText("Name");
		email_header.setText("User Email");
		phone_number_header.setText("Phone Number");
		judge_panel.add(name_header, BorderLayout.NORTH);
		judge_panel.add(email_header, BorderLayout.NORTH);
		judge_panel.add(phone_number_header, BorderLayout.NORTH);
		for (Judge j : judgeList) {
			JLabel name = new JLabel();
			JLabel email = new JLabel();
			JLabel number = new JLabel();
			name.setPreferredSize(LABEL_SIZE);
			email.setPreferredSize(LABEL_SIZE);
			number.setPreferredSize(LABEL_SIZE);
			name.setText(j.getName());
			email.setText(j.getEmail());
			number.setText(j.getPhoneNumber());
			judge_panel.add(name);
			judge_panel.add(email);
			judge_panel.add(number);
		}// close for loop
		scrollPane = new JScrollPane(judge_panel);
		add(scrollPane);
	}
	
	/**
	 * Populates the frame with a list of contest winners.  For use by anyone using the program.
	 */
		
	public void showContestWinners() {
		name_header.setText("Name");
		email_header.setText("User Email");
		phone_number_header.setText("Phone Number");
		winners_panel.add(name_header, BorderLayout.NORTH);
		winners_panel.add(email_header, BorderLayout.NORTH);
		winners_panel.add(phone_number_header, BorderLayout.NORTH);
		for (Contestant c : contestWinners) {
			JLabel name = new JLabel();
			JLabel email = new JLabel();
			JLabel number = new JLabel();
			name.setPreferredSize(LABEL_SIZE);
			email.setPreferredSize(LABEL_SIZE);
			number.setPreferredSize(LABEL_SIZE);
			name.setText(c.getName());
			email.setText(c.getEmail());
			number.setText(c.getPhoneNumber());
			winners_panel.add(name);
			winners_panel.add(email);
			winners_panel.add(number);
		}// close for loop
		scrollPane = new JScrollPane(winners_panel);
		add(scrollPane);
			
	}

	/**
	 * Shows the list of contestant entries.
	 */
	
	private void showEntryList() {
		String reg_number;
		JLabel name = new JLabel();
		name.setPreferredSize(ENTRY_LABEL_SIZE);
		JPanel header_panel = new JPanel();
		Entry first;
		Entry second;
		Entry third;
		name.setText("Name");
		reg_number_header.setText("Registration Number");
		entry_1_header.setText("First Entry");
		entry_2_header.setText("Second Entry");
		entry_3_header.setText("Third Entry");
		header_panel.add(name);
		header_panel.add(reg_number_header);
		header_panel.add(entry_1_header);
		header_panel.add(entry_2_header);
		header_panel.add(entry_3_header);
		entry_panel.add(header_panel, BorderLayout.CENTER);

		for (Contestant c : contestantList) {
			JPanel panel = new JPanel();
			first = c.getEntry1();
			final String first_desc = first.getDescription();
			final String first_cat = first.getCategory();
			final boolean first_rel = first.getRelease();
			final int[][] first_design = first.getDesign();
			second = c.getEntry2();
			final String second_cat = second.getCategory();
			final boolean second_rel = second.getRelease();
			final String second_desc = second.getDescription();
			final int[][] second_design = second.getDesign();
			third = c.getEntry3();
			final String third_cat = third.getCategory();
			final boolean third_rel = third.getRelease();
			final String third_desc = third.getDescription();
			final int[][] third_design = third.getDesign();

			JLabel person = new JLabel();
			JLabel reg = new JLabel();
			JButton desc1 = new JButton();
			if (first_design == null) {
				desc1.setEnabled(false);
			}
			JButton desc2 = new JButton();
			if (second_design == null) {
				desc2.setEnabled(false);
			}
			JButton desc3 = new JButton();
			if (third_design == null) {
				desc3.setEnabled(false);
			}
			desc1.addActionListener(new ActionListener()

	        {
	          @Override
			public void actionPerformed(final ActionEvent the_event)
	        
	          {
	        	showEntry(first_desc, first_cat, first_rel, first_design);  
	          }

	        });

			desc2.addActionListener(new ActionListener()

	        {
	          @Override
			public void actionPerformed(final ActionEvent the_event)
	        
	          {
	        	  showEntry(second_desc, second_cat, second_rel, second_design);
	          }

	        });

			desc3.addActionListener(new ActionListener()

	        {
	          @Override
			public void actionPerformed(final ActionEvent the_event)
	        
	          {
	        	showEntry(third_desc, third_cat, third_rel, third_design);  
	          }

	        });

			
			person.setPreferredSize(ENTRY_LABEL_SIZE);
			reg.setPreferredSize(ENTRY_LABEL_SIZE);
			desc1.setPreferredSize(ENTRY_LABEL_SIZE);
			desc2.setPreferredSize(ENTRY_LABEL_SIZE);
			desc3.setPreferredSize(ENTRY_LABEL_SIZE);
			person.setText(c.getName());
			reg_number = Integer.toString(c.getRegistrationNumber());
			reg.setText(reg_number);
			desc1.setText("View Entry");
			desc2.setText("View Entry");
			desc3.setText("View Entry");
			
			panel.add(person);
			panel.add(reg);
			panel.add(desc1);
			panel.add(desc2);
			panel.add(desc3);
			entry_panel.add(panel, BorderLayout.CENTER);
		}// close for loop
		scrollPane = new JScrollPane(entry_panel);
		add(scrollPane);
		
	}

	
	/**
	 * Returns the choice.
	 * @return choice
	 */
	
	public int getChoice(){
		return choice;
	}
	
	/**
	 * Sets the given choice. 1 for contestant list, 2 for judge list, 3 for entry list, 
	 * 4 for contest winners.
	 */
	
	public void SetChoice(int incoming){
		choice = incoming;
	}
	
	public void showEntry(String description, String category, boolean release, int[][] design) {
		JFrame entry = new JFrame();
		entry.pack();
		entry.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		entry.setLocationRelativeTo(null);
		entry.setResizable(false);
	    entry.setVisible(true);
		JPanel panel = new JPanel();
		JPanel drawing_area = new JPanel();
		Container grid = new JPanel(new GridLayout(3,1));
		drawing_area.setPreferredSize(new Dimension(400,400));
		drawing_area.setBackground(Color.BLACK);
		drawing_area.setLayout(new GridLayout(16,16));//////////////
		//panel.setPreferredSize(PANEL_SIZE);
		JLabel desc_label = new JLabel(description);
		JLabel category_label = new JLabel("Category: " + category);
		JLabel release_label = new JLabel();
		if (release) {
			release_label.setText("This design is available for Sponsors to use.");
		}
		else {
			release_label.setText("This design is not available for Sponsors to use.");
		}
		grid.add(desc_label);
		grid.add(category_label);
		grid.add(release_label);
		panel.add(grid, BorderLayout.CENTER);
		//panel.add(drawing_area, BorderLayout.WEST);
//		drawDesign(design, panel, entry);
		drawDesign(design, drawing_area, entry);
		
		entry.add(panel);
		entry.pack();
	}
	
	public void drawDesign(int[][] design, JPanel the_panel, JFrame frame) {
		int pos = 0;
		PatternSquare[] squares = new PatternSquare[16*16];
		for(int i = 0; i < 16; i++){
			for(int j = 0; j < 16; j++){
				PatternSquare panel = new PatternSquare(i, j);
				if (design[i][j] == 1) {
					panel.setBlack();
				}
				else {
					panel.setWhite();
				}
				the_panel.add(panel);
				squares[pos] = panel;
				pos++;
			}
		}
		
		frame.add(the_panel, BorderLayout.WEST);
	
	}
}

